/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.actions;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.ipo.mm.ejb.entities.Actions;
import org.ipo.mm.ejb.facade.actions.ActionsFacadeLocal;
import org.ipo.mm.web.formbeans.ActionsListBean;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author makcro
 */
public class EditActionsAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ActionForward actionForward = null;

        MessageResources mResources = getResources(request);
        String deleteOperaton = mResources.getMessage(getLocale(request), "gen_delete");
        String updateOperation = mResources.getMessage(getLocale(request), "gen_update");

        ActionsListBean actionsBean = (ActionsListBean) form;

        String operation = actionsBean.getOperation();

        if (deleteOperaton.equals(operation)) {
            actionForward = executeDelete(actionsBean, mapping);
        } else if (updateOperation.equals(operation)) {
            actionForward = executeUpdate(actionsBean, mapping, request);
        }

        return actionForward;
    }

    private ActionForward executeDelete(ActionsListBean ab, ActionMapping mapping) {
        ActionForward actionForward = null;

        String[] selectedAction = ab.getSelectedActions();

        if (selectedAction != null) {

            ActionsFacadeLocal afl = lookupActionsFacade();


            for (int i = 0; i < selectedAction.length; i++) {
                String actonIdString = selectedAction[i];

                Actions a = afl.find(Integer.parseInt(actonIdString));
                a.setDeleted(true);

                afl.edit(a);

            }
        }
        actionForward = mapping.findForward(ConstantManager.NAVIGATION_ACTIONS_LIST_RELOAD);

        return actionForward;
    }

    private ActionsFacadeLocal lookupActionsFacade() {
        try {
            Context c = new InitialContext();
            return (ActionsFacadeLocal) c.lookup("java:comp/env/ActionsFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ActionForward executeUpdate(ActionsListBean actionsBean, ActionMapping mapping, HttpServletRequest request) {
        ActionForward actionForward = null;

        Actions[] actions = actionsBean.getActions();

        ActionsFacadeLocal afl = lookupActionsFacade();

        for (Actions actions1 : actions) {
            afl.edit(actions1);
        }

        actionForward = mapping.findForward(ConstantManager.NAVIGATION_ACTIONS_LIST_RELOAD);

        return actionForward;
    }
}
